package Lab15;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import static Lab15.Menu.planeMenu;

public class PlaneFuncImpl implements PlaneFunc{
    //获取单例
    List<PlaneInfo> planeInfos = PlaneInfoSigleList.getPlaneInfos();
    @Override
    public void addPlaneInfo() {
        PlaneInfo planeInfo = new PlaneInfo();
        System.out.println("录入航班信息：");
        System.out.println("航班编号：");
        Scanner sc = new Scanner(System.in);
        String planeId = sc.next();
        planeInfo.setPlaneId(planeId);

        System.out.println("航线：");
        System.out.println("出发地：");
        String routeStart = sc.next();
        planeInfo.setRouteStart(routeStart);
        System.out.println("目的地：");
        String routeEnd = sc.next();
        planeInfo.setRouteEnd(routeEnd);

        System.out.println("时间(输入数字)：");
        System.out.println("星期一：1  星期二：2  星期三：3  星期四：4  星期五：5  星期六：6  星期日：7");
        int day = sc.nextInt();
        String time = numToString(day);
        planeInfo.setTime(time);

        System.out.println("座位数：");
        int seatNum = sc.nextInt();
        planeInfo.setSeatNum(seatNum);
//        PlaneInfoSigleList.getPlaneInfos().add(planeInfo);
        planeInfos.add(planeInfo);
        System.out.println("航班编号\t航线\t\t\t时间\t\t座位数");
        System.out.println(planeInfo);
        System.out.println("录入成功！");

    }

    private String numToString (int day) {
        switch (day) {
            case 0:
                return "back";
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            case 7:
                return "星期日";
            case 9:
                return "";
            default:
                return "error";
        }
    }

//    @Override
//    public void deletePlaneInfo() {
//        System.out.println("请输入要删除的航班编号：");
//
//    }

    // 查询: 多种查询方式（航班好，起点，终点，飞行时间，多种组合）
    @Override
    public void searchPlaneInfo() {
        System.out.println("请输入要查询方式：");
        System.out.println("***  1. 航班编号    2. 航班起点  ***");
        System.out.println("***  3. 航班终点    4. 航班时间  ***");
        System.out.println("***  5. 混合查询    6. 退出查询  ***");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        List<PlaneInfo> searchWay = new ArrayList<>();
        switch (choice) {
            case 1:
                do {
                    System.out.println("请输入航班编号：");
                    String planeId = sc.next();
//                    searchWay = searchWay1(planeId);
                    searchWay = searchWay5(planeId, "", "", "");
                    if (searchWay.size() != 0) {
                        showPlaneInfo(searchWay);
                        break;
                    }
                    System.out.println("没有找到相关航班！请重新输入：");
                } while (searchWay.size() == 0);

                break;
            case 2:
                do {
                    System.out.println("请输入航班起点：");
                    String routeStart = sc.next();
//                    searchWay = searchWay2(routeStart);
                    searchWay = searchWay5("", routeStart, "", "");
                    if (searchWay.size() != 0) {
                        showPlaneInfo(searchWay);
                        break;
                    }
                    System.out.println("没有找到相关航班！请重新输入：");
                } while (searchWay.size() == 0);
                break;
            case 3:
                do {
                    System.out.println("请输入航班终点：");
                    String routeEnd = sc.next();
//                    searchWay = searchWay3(routeEnd);
                    searchWay = searchWay5("", "",routeEnd,"");
                    if (searchWay.size() != 0) {
                        showPlaneInfo(searchWay);
                        break;
                    }
                    System.out.println("没有找到相关航班！请重新输入：");
                } while (searchWay.size() == 0);
                break;
            case 4:
                do {
                    System.out.println("请输入航班时间：");
                    System.out.println("星期一：1  星期二：2  星期三：3  星期四：4  星期五：5  星期六：6  星期日：7");
                    String time = numToString(sc.nextInt());
//                    searchWay =  searchWay4(time);//searchWay5(String planeId, String routeStart, String routeEnd,String time){
                    searchWay = searchWay5("", "", "",time);
                    if (searchWay.size() != 0) {
                        showPlaneInfo(searchWay);
                        break;
                    }
                    System.out.println("没有找到相关航班！请重新输入：");
                } while (searchWay.size()==0);
                break;
            case 5:
                do {
                    sc.nextLine();//跳过上一个查询方式的回车
                    System.out.println("请输入航班编号（可为空，直接回车）：");
                    String planeId = sc.nextLine();
                    System.out.println("请输入航班起点（可为空，直接回车）：");
                    String routeStart = sc.nextLine();
                    System.out.println("请输入航班终点（可为空，直接回车）：");
                    String routeEnd = sc.nextLine();
                    System.out.println("星期一：1  星期二：2  星期三：3  星期四：4  星期五：5  星期六：6  星期日：7（输入9为空，输入0为退出）");
                    String time = numToString(sc.nextInt());
                    if (time.equals("back")) {
                        try {
                            planeMenu();//不抛出，直接打印
                            return;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (routeStart.equals("error")) {
                        try {
                            System.out.println("输入错误！退出.....");
                            planeMenu();//不抛出，直接打印
                            return;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    searchWay = searchWay5(planeId, routeStart, routeEnd,time);
                    if (searchWay.size() != 0) {
                        showPlaneInfo(searchWay);
                        break;
                    }
                    System.out.println("没有找到相关航班！请重新输入：");
                } while (searchWay.size()==0);
                break;
            default:
                System.out.println("输入错误！");
        }

    }

    //根据id只能搜索到唯一一条
    private List<PlaneInfo> searchWay1(String planeId) {
        List<PlaneInfo> searchWay1 = new ArrayList<>();
        for (PlaneInfo planeInfo : planeInfos) {
            if (planeInfo.getPlaneId().equals(planeId)) {
                searchWay1.add(planeInfo);
            }
        }
        return searchWay1;
    }
    private List<PlaneInfo> searchWay2(String routeStart) {
        List<PlaneInfo> searchWay2 = new ArrayList<>();
        for (PlaneInfo planeInfo : planeInfos) {
            if (planeInfo.getRouteStart().equals(routeStart)) {
                searchWay2.add(planeInfo);
            }
        }
        return searchWay2;
    }
    private List<PlaneInfo> searchWay3(String routeEnd) {
        List<PlaneInfo> searchWay3 = new ArrayList<>();
        for (PlaneInfo planeInfo : planeInfos) {
            if (planeInfo.getRouteEnd().equals(routeEnd)) {
                searchWay3.add(planeInfo);
            }
        }
        return searchWay3;
    }
    private List<PlaneInfo> searchWay4(String time) {
        List<PlaneInfo> searchWay4 = new ArrayList<>();
        for (PlaneInfo planeInfo : planeInfos) {
            if (planeInfo.getTime().equals(time)) {
                searchWay4.add(planeInfo);
            }
        }
        return searchWay4;
    }

    //这一个方法输入空就可以完成上面的方法
    private List<PlaneInfo> searchWay5(String planeId, String routeStart, String routeEnd,String time){
        List<PlaneInfo> printSearch = new ArrayList<>();
        //如果为空，或者匹配，则输出
        //直接回车会输入""
        for(PlaneInfo planeInfo : planeInfos){
            if(planeInfo.getPlaneId().equals(planeId) || planeId.equals("")){
                if(planeInfo.getRouteStart().equals(routeStart) || routeStart.equals("")){
                    if(planeInfo.getRouteEnd().equals(routeEnd) || routeEnd.equals("")){
                        if(planeInfo.getTime().equals(time) || time.equals("")){
                            printSearch.add(planeInfo);
                        }
                    }
                }
            }
        }
        return printSearch;
    }

    @Override
    public void updatePlaneInfo() {
        //航班编号是唯一标识符
        System.out.println("请输入要修改的航班编号：");
        //展示可修改的航班信息,即所有航班信息
        showPlaneInfo(planeInfos);
        Scanner sc = new Scanner(System.in);
        String planeId = sc.next();
//        planeInfos = searchWay1(planeId);
        planeInfos = searchWay5(planeId, "", "", "");
        if(planeInfos == null || planeInfos.size() == 0){
            System.out.println("没有该航班！退出操作！");
            return;
        }
        PlaneInfo planeInfo = planeInfos.get(0);
        updatePlaneTicketCounts();
        //如果航班有人选就不能修改
        if(planeInfo.getIsChoose() != 0){
            System.out.println("该航班有人选择，不能修改！");
            return;
        }
        System.out.println("航班编号\t航线\t\t\t时间\t\t座位数");
        System.out.println(planeInfo);
        if (planeInfo.getPlaneId().equals(planeId)) {
            System.out.println("请输入新的航班编号：");
            String newPlaneId = sc.next();
            planeInfo.setPlaneId(newPlaneId);
            System.out.println("请输入新的航线起点：");
            String routeStart = sc.next();
            planeInfo.setRouteStart(routeStart);
            System.out.println("请输入新的航线终点：");
            String routeEnd = sc.next();
            planeInfo.setRouteEnd(routeEnd);
            System.out.println("请输入新的时间：");
            System.out.println("星期一：1  星期二：2  星期三：3  星期四：4  星期五：5  星期六：6  星期日：7（不可为空）");
            String time = numToString(sc.nextInt());
            planeInfo.setTime(time);
            System.out.println("请输入新的座位数：");
            int seatNum = sc.nextInt();
            planeInfo.setSeatNum(seatNum);
        }
        System.out.println("航班编号\t航线\t\t\t时间\t\t座位数");
        System.out.println(planeInfo);
        System.out.println("修改成功！!");

    }
    public void showPlaneInfo(List<PlaneInfo> planeInfosTemp) {
        //显示所有航班信息
        //用文件保存，从文件读取
//        System.out.println("\n所有航班信息：");
        System.out.println("航班编号\t航线\t\t\t时间\t\t座位数");
        for (PlaneInfo planeInfo : planeInfosTemp) {
            System.out.println(planeInfo.getPlaneId() + "\t" + planeInfo.getRouteStart() +"->"+planeInfo.getRouteEnd()  + "\t" + planeInfo.getTime() + "\t" + planeInfo.getSeatNum());
        }
        System.out.println();
    }

    //创建一个私有方法用来记录是否选择寒冰
    private void updatePlaneTicketCounts() {
        // 首先重置所有航班的isChoose为0
        for (PlaneInfo planeInfo : planeInfos) {
            planeInfo.setIsChoose(0);
        }

        // 遍历所有客户信息
        List<CustomerInfo> customerInfos = CustomerSigleList.getCustomerInfos();
        for (CustomerInfo customer : customerInfos) {
            // 遍历每个客户的航班信息
            List<PlaneInfo> customerPlanes = customer.getPlaneInfos();
            if (customerPlanes != null) {
                for (PlaneInfo customerPlane : customerPlanes) {
                    // 在航班列表中找到对应的航班并增加isChoose计数
                    for (PlaneInfo planeInfo : planeInfos) {
                        if (planeInfo.getPlaneId().equals(customerPlane.getPlaneId())) {
                            planeInfo.setIsChoose(planeInfo.getIsChoose() + 1);//给对应航班修改了字段
                            break;
                        }
                    }
                }
            }
        }
    }
}